package com.baizhi.xxn.service;

import com.baizhi.xxn.dao.AdminDao;
import com.baizhi.xxn.entity.Admin;
import com.baizhi.xxn.utils.ImageCodeUtil;
import com.baizhi.xxn.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void register(Admin admin) {
        String password = admin.getPassword();
        //调用工具获取随机盐
        String salt = Md5Utils.getSalt(6);
        //对原始密码加随机盐
        String saltPaw=salt + password + salt;
        //调用工具对随机盐进行md5加密
        String md5Code = Md5Utils.getMd5Code(saltPaw);
        //对传过来的管理数据进行加工
        admin.setPassword(md5Code);
        admin.setSalt(salt);
        adminDao.insert(admin);
    }

    //验证码
    @Override
    public Map<String,String> getImageCodes() throws IOException {
        Map<String,String> map=new HashMap<>();
        //调用工具类，获取随机验证码字符
        String code = ImageCodeUtil.getSecurityCode();
        //调用工具类，根据随机码生成图片
        String imgBase64 = ImageCodeUtil.careateImgBase64(code);
        //把64图片信息存入map
        map.put("imgCode",imgBase64);
        //获取uuid
        String uuid = UUID.randomUUID().toString();
        //把uuid存入map
        map.put("token",uuid);
        //往reser中存储
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valuesOps = redisTemplate.opsForValue();
        valuesOps.set("CODE_"+uuid,code,1, TimeUnit.MINUTES);
        return map;
    }
    //登陆检查
    public Map<String,String> login(@RequestBody Admin admin,String token){
        Map<String,String> map=new HashMap<>();
        //往reser中存储
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valuesOps = redisTemplate.opsForValue();
        Object obj = valuesOps.get("CODE_" + token);
        //检查验证码
        if (obj==null){//验证码过期
            map.put("message","验证码过期");
            map.put("state","error");
            return map;
        }
        String serverCode = obj.toString();
        if (!admin.getEnCode().equals(serverCode)){//验证码过期
            map.put("message","验证码错误");
            map.put("state","error");
            return map;
        }
        //验证码正确，检查密码
        Admin user = adminDao.selectName(admin.getUsername());
        if(user==null){
            map.put("message","用户不存在");
            map.put("state","error");
            return map;
        }else {
            if (user.getStatus()=="0"){//用户0冻结
                map.put("message","用户已被冻结");
                map.put("state","error");
                return map;
            }
            String pwb = admin.getPassword();
            String saltPwb=user.getSalt()+ pwb + user.getSalt();
            String md5Code = Md5Utils.getMd5Code(saltPwb);
            System.out.println(pwb);
            System.out.println(saltPwb);
            if (md5Code.equals(user.getPassword())){
                map.put("message",token);
                map.put("state","success");
                valuesOps.set("TOKEN_"+token,user);
            }else {
                map.put("message","密码错误");
                map.put("state","error");
                return map;
            }
        }
        return map;
    }
    //获取登陆用户信息
    public Admin getUserInfo(String token){
        //往reser中存储
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valuesOps = redisTemplate.opsForValue();
        return (Admin) valuesOps.get("TOKEN_"+token);
    }
    //获取登陆用户信息
    public void logout(String token){
        //往reser中存储
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.delete("TOKEN_"+token);
    }
}
